// META: title=test WebNN API where operation
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long

'use strict';

// https://www.w3.org/TR/webnn/#api-mlgraphbuilder-where
// Select the values from the input or the other tensor depending on the
// corresponding boolean values of the condition tensor.
//
// MLOperand where(MLOperand condition, MLOperand trueValue, MLOperand
//                 falseValue);


const getWherePrecisionTolerance = (graphResources) => {
  const toleranceValueDict = {float32: 0, float16: 0};
  const expectedDataType =
      getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs);
  return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]};
};

const whereTests = [
  {
    'name': 'where float32 0D scalars',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [247],
          'descriptor': {'dimensions': [], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [-22.83924102783203],
          'descriptor': {'dimensions': [], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [-50.02232360839844],
          'descriptor': {'dimensions': [], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [-22.83924102783203],
          'descriptor': {'dimensions': [], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 1D constant tensors',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'uint8'},
          'constant': true
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'},
          'constant': true
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'},
          'constant': true
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,   -1.5351932048797607,
            -21.683168411254883, 29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  65.5730209350586,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 1D tensors',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,   -1.5351932048797607,
            -21.683168411254883, 29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  65.5730209350586,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [24], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 2D tensors',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [4, 6], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [4, 6], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [4, 6], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,   -1.5351932048797607,
            -21.683168411254883, 29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  65.5730209350586,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [4, 6], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 3D tensors',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 3, 4], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 3, 4], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 3, 4], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,   -1.5351932048797607,
            -21.683168411254883, 29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  65.5730209350586,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 3, 4], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,   -1.5351932048797607,
            -21.683168411254883, 29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  65.5730209350586,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 5D tensors',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 2, 1, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 1, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 1, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,   -1.5351932048797607,
            -21.683168411254883, 29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  65.5730209350586,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 1, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast condition 0D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [247],
          'descriptor': {'dimensions': [], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast condition 1D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [253, 222, 0],
          'descriptor': {'dimensions': [3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   89.99047088623047,  -39.82423782348633,
            -83.77035522460938,  29.59670639038086,  -46.46003341674805,
            -6.663239002227783,  -83.15333557128906, 35.14354705810547,
            -45.470706939697266, 50.38861083984375,  60.741416931152344,
            47.503013610839844,  -21.95842933654785, 34.315250396728516,
            -76.8262710571289,   67.0030746459961,   91.12223052978516,
            -29.081029891967773, -12.19106674194336, 18.877336502075195,
            -23.302522659301758, 30.851404190063477, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast condition 2D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [103, 0],
          'descriptor': {'dimensions': [2, 1], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -21.683168411254883, -67.3449478149414,  -46.46003341674805,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            11.960981369018555,  76.65287780761719,  60.741416931152344,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            7.7088470458984375,  -82.2353286743164,  91.12223052978516,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            62.31299591064453,   48.867103576660156, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast condition 3D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [235, 0, 93, 213, 0, 117],
          'descriptor': {'dimensions': [1, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   66.7642593383789,    -1.5351932048797607,
            -83.77035522460938,  -67.3449478149414,   -37.10446548461914,
            -6.663239002227783,  65.5730209350586,    -90.47624206542969,
            -45.470706939697266, 76.65287780761719,   46.510799407958984,
            47.503013610839844,  -30.397233963012695, 42.89878463745117,
            -76.8262710571289,   -82.2353286743164,   40.09717559814453,
            -29.081029891967773, 74.97810363769531,   -10.8577880859375,
            -23.302522659301758, 48.867103576660156,  -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast condition 4D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [0, 165, 0, 90],
          'descriptor': {'dimensions': [1, 2, 2, 1], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -83.77035522460938,  29.59670639038086,   -37.10446548461914,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            -45.470706939697266, 50.38861083984375,   46.510799407958984,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            -76.8262710571289,   67.0030746459961,    40.09717559814453,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            -23.302522659301758, 30.851404190063477,  -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast trueValues 2D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            38.77214050292969, 18.20477294921875, -11.542049407958984,
            -82.74693298339844, -18.61202621459961, -75.77534484863281
          ],
          'descriptor': {'dimensions': [2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            38.77214050292969,   66.7642593383789,   -11.542049407958984,
            -21.683168411254883, -18.61202621459961, -75.77534484863281,
            38.77214050292969,   65.5730209350586,   -11.542049407958984,
            -82.74693298339844,  -18.61202621459961, -75.77534484863281,
            38.77214050292969,   18.20477294921875,  -11.542049407958984,
            -82.74693298339844,  -18.61202621459961, -75.77534484863281,
            38.77214050292969,   18.20477294921875,  18.877336502075195,
            -82.74693298339844,  -18.61202621459961, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast trueValues 4D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            -29.164173126220703, 32.200286865234375, -6.146966457366943,
            -24.822790145874023
          ],
          'descriptor': {'dimensions': [2, 2, 1, 1], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            90.44255065917969,   66.7642593383789,    -39.82423782348633,
            -21.683168411254883, -67.3449478149414,   -46.46003341674805,
            -55.907073974609375, 65.5730209350586,    35.14354705810547,
            11.960981369018555,  76.65287780761719,   60.741416931152344,
            -93.93305206298828,  -30.397233963012695, 34.315250396728516,
            7.7088470458984375,  -82.2353286743164,   91.12223052978516,
            22.937334060668945,  74.97810363769531,   18.877336502075195,
            62.31299591064453,   48.867103576660156,  -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            -29.164173126220703, 66.7642593383789,    -29.164173126220703,
            -21.683168411254883, -29.164173126220703, -29.164173126220703,
            32.200286865234375,  65.5730209350586,    32.200286865234375,
            32.200286865234375,  32.200286865234375,  32.200286865234375,
            -6.146966457366943,  -6.146966457366943,  -6.146966457366943,
            -6.146966457366943,  -6.146966457366943,  -6.146966457366943,
            -24.822790145874023, -24.822790145874023, 18.877336502075195,
            -24.822790145874023, -24.822790145874023, -75.41789245605469
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast falseValues 3D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            -86.59848022460938, -67.72773742675781, -30.731334686279297,
            -93.49223327636719
          ],
          'descriptor': {'dimensions': [2, 2, 1], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   -86.59848022460938,  -1.5351932048797607,
            -67.72773742675781,  29.59670639038086,   -37.10446548461914,
            -6.663239002227783,  -30.731334686279297, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,   46.510799407958984,
            47.503013610839844,  -21.95842933654785,  42.89878463745117,
            -76.8262710571289,   67.0030746459961,    40.09717559814453,
            -29.081029891967773, -12.19106674194336,  -30.731334686279297,
            -23.302522659301758, 30.851404190063477,  -93.49223327636719
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors only broadcast falseValues 4D to 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [
            241, 0,   85,  0,   227, 51, 202, 0,  104, 227, 129, 129,
            175, 134, 130, 140, 103, 46, 158, 17, 0,   41,  94,  0
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            70.27151489257812,   89.99047088623047,  -1.5351932048797607,
            -83.77035522460938,  29.59670639038086,  -37.10446548461914,
            -6.663239002227783,  -83.15333557128906, -90.47624206542969,
            -45.470706939697266, 50.38861083984375,  46.510799407958984,
            47.503013610839844,  -21.95842933654785, 42.89878463745117,
            -76.8262710571289,   67.0030746459961,   40.09717559814453,
            -29.081029891967773, -12.19106674194336, -10.8577880859375,
            -23.302522659301758, 30.851404190063477, -58.82704544067383
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            17.777414321899414, -1.9890096187591553, -57.28207015991211,
            -80.01390838623047, 66.87323760986328, -31.328258514404297
          ],
          'descriptor': {'dimensions': [1, 2, 1, 3], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            70.27151489257812,   -1.9890096187591553, -1.5351932048797607,
            17.777414321899414,  29.59670639038086,   -37.10446548461914,
            -6.663239002227783,  66.87323760986328,   -90.47624206542969,
            -45.470706939697266, 50.38861083984375,   46.510799407958984,
            47.503013610839844,  -21.95842933654785,  42.89878463745117,
            -76.8262710571289,   67.0030746459961,    40.09717559814453,
            -29.081029891967773, -12.19106674194336,  -31.328258514404297,
            -23.302522659301758, 30.851404190063477,  -31.328258514404297
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  },
  {
    'name': 'where float32 4D tensors all broadcast 4D',
    'graph': {
      'inputs': {
        'inputCondition': {
          'data': [58, 217],
          'descriptor': {'dimensions': [2, 1, 1, 1], 'dataType': 'uint8'}
        },
        'inputTrueValue': {
          'data': [
            38.77214050292969, 18.20477294921875, -11.542049407958984,
            -82.74693298339844, -18.61202621459961, -75.77534484863281
          ],
          'descriptor': {'dimensions': [2, 3], 'dataType': 'float32'}
        },
        'inputFalseValue': {
          'data': [
            -86.59848022460938, -67.72773742675781, -30.731334686279297,
            -93.49223327636719
          ],
          'descriptor': {'dimensions': [2, 2, 1], 'dataType': 'float32'}
        }
      },
      'operators': [{
        'name': 'where',
        'arguments': [
          {'condition': 'inputCondition'}, {'trueValue': 'inputTrueValue'},
          {'falseValue': 'inputFalseValue'}
        ],
        'outputs': 'output'
      }],
      'expectedOutputs': {
        'whereOutput': {
          'data': [
            38.77214050292969,  18.20477294921875,  -11.542049407958984,
            -82.74693298339844, -18.61202621459961, -75.77534484863281,
            38.77214050292969,  18.20477294921875,  -11.542049407958984,
            -82.74693298339844, -18.61202621459961, -75.77534484863281,
            38.77214050292969,  18.20477294921875,  -11.542049407958984,
            -82.74693298339844, -18.61202621459961, -75.77534484863281,
            38.77214050292969,  18.20477294921875,  -11.542049407958984,
            -82.74693298339844, -18.61202621459961, -75.77534484863281
          ],
          'descriptor': {'dimensions': [2, 2, 2, 3], 'dataType': 'float32'}
        }
      }
    }
  }
];

if (navigator.ml) {
  whereTests.forEach((test) => {
    webnn_conformance_test(
        buildGraphAndCompute, getWherePrecisionTolerance, test);
  });
} else {
  test(() => assert_implements(navigator.ml, 'missing navigator.ml'));
}
